home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / tri.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.8 KB  |  243 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <GL/glx.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <X11/keysym.h>
  21.  
  22. static int attributes[] = {
  23.     GLX_RGBA,
  24.     GLX_RED_SIZE, 1,
  25.     GLX_GREEN_SIZE, 1,
  26.     GLX_BLUE_SIZE, 1,
  27.     None,
  28. };
  29.  
  30. int width = 200, height = 200;
  31.  
  32. static void DoDisplay(GLfloat tx, GLfloat ty, GLfloat rx, GLfloat rz)
  33. {
  34.     glMatrixMode(GL_PROJECTION);
  35.     glLoadIdentity();
  36.     glOrtho(-0.5, width - 0.5, -0.5, height - 0.5, 0.01, 1000.0);
  37.     glMatrixMode(GL_MODELVIEW);
  38.     glLoadIdentity();
  39.     glTranslatef(100 + tx, 100 + ty, -10);
  40.     glRotatef(rx, 1, 0, 0);
  41.     glRotatef(rz, 0, 0, 1);
  42.     glScalef(20, 20, 20);
  43.  
  44.     glViewport(0, 0, width, height);
  45.     glClearColor(0.25, 0.25, 0.25, 1.0);
  46.     glClear(GL_COLOR_BUFFER_BIT);
  47.     glShadeModel(GL_FLAT);
  48.  
  49.     /* Draw a bunch of triangles that converge on a single point */
  50.     glBegin(GL_TRIANGLE_FAN);
  51.     glColor3f(1, 0, 0);
  52.     glVertex2f(0, 0);
  53.     glVertex2f(-1, -0.5);
  54.     glVertex2f(-1, -0.25);
  55.     glColor3f(0, 1, 0);
  56.     glVertex2f(-1, 0);
  57.     glColor3f(0, 0, 1);
  58.     glVertex2f(-1, 0.25);
  59.     glColor3f(1, 1, 1);
  60.     glVertex2f(-1, 0.5);
  61.     glEnd();
  62.  
  63.     glBegin(GL_TRIANGLE_FAN);
  64.     glColor3f(1, 0, 0);
  65.     glVertex2f(0, 0);
  66.     glVertex2f(-0.5, 1);
  67.     glVertex2f(-0.25, 1);
  68.     glColor3f(0, 1, 0);
  69.     glVertex2f(0, 1);
  70.     glColor3f(0, 0, 1);
  71.     glVertex2f(0.25, 1);
  72.     glColor3f(1, 1, 1);
  73.     glVertex2f(0.5, 1);
  74.     glEnd();
  75.  
  76.     glBegin(GL_TRIANGLE_FAN);
  77.     glColor3f(1, 0, 0);
  78.     glVertex2f(0, 0);
  79.     glVertex2f(1, 0.5);
  80.     glVertex2f(1, 0.25);
  81.     glColor3f(0, 1, 0);
  82.     glVertex2f(1, 0);
  83.     glColor3f(0, 0, 1);
  84.     glVertex2f(1, -0.25);
  85.     glColor3f(1, 1, 1);
  86.     glVertex2f(1, -0.5);
  87.     glEnd();
  88.  
  89.     glBegin(GL_TRIANGLE_FAN);
  90.     glColor3f(1, 0, 0);
  91.     glVertex2f(0, 0);
  92.     glVertex2f(-0.5, -1);
  93.     glVertex2f(-0.25, -1);
  94.     glColor3f(0, 1, 0);
  95.     glVertex2f(0, -1);
  96.     glColor3f(0, 0, 1);
  97.     glVertex2f(0.25, -1);
  98.     glColor3f(1, 1, 1);
  99.     glVertex2f(0.5, -1);
  100.     glEnd();
  101. }
  102.  
  103. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  104. {
  105.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  106.     return GL_TRUE;
  107.     }
  108.     return GL_FALSE;
  109. }
  110.  
  111. int main(int argc, char *argv[])
  112. {
  113.     XVisualInfo *vi;
  114.     Display *dpy;
  115.     Colormap cmap;
  116.     Window window;
  117.     XSetWindowAttributes swa;
  118.     GLXContext cx;
  119.     XEvent event;
  120.     GLboolean needDisplay;
  121.     GLfloat tx = 0;
  122.     GLfloat ty = 0;
  123.     GLfloat rx = 0;
  124.     GLfloat rz = 0;
  125.  
  126.     dpy = XOpenDisplay(0);
  127.     if (!dpy) {
  128.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  129.     return -1;
  130.     }
  131.  
  132.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
  133.     if (!vi) {
  134.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  135.         getenv("DISPLAY"));
  136.     return -1;
  137.     }
  138.  
  139.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  140.                AllocNone);
  141.     swa.border_pixel = 0;
  142.     swa.colormap = cmap;
  143.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  144.     | KeyReleaseMask;
  145.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  146.                width, height,
  147.                0, vi->depth, InputOutput, vi->visual,
  148.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  149.     XSetStandardProperties(dpy, window, "tri", "tri", None, argv, argc, NULL);
  150.     XSetWMColormapWindows(dpy, window, &window, 1);
  151.     XMapWindow(dpy, window);
  152.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  153.  
  154.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  155.     if (!glXMakeCurrent(dpy, window, cx)) {
  156.     fprintf(stderr, "Can't make window current to context\n");
  157.     return -1;
  158.     }
  159.  
  160.     needDisplay = GL_TRUE;
  161.     for (;;) {
  162.     do {
  163.         XNextEvent(dpy, &event);
  164.         switch (event.type) {
  165.           case Expose:
  166.         needDisplay = GL_TRUE;
  167.         break;
  168.           case ConfigureNotify:
  169.         width = event.xconfigure.width;
  170.         height = event.xconfigure.height;
  171.         needDisplay = GL_TRUE;
  172.         break;
  173.           case KeyPress:
  174.         {
  175.             char buf[100];
  176.             int rv;
  177.             KeySym ks;
  178.  
  179.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  180.             switch (ks) {
  181.               case XK_p:
  182.               case XK_P:
  183.             glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  184.             needDisplay = GL_TRUE;
  185.             break;
  186.               case XK_l:
  187.               case XK_L:
  188.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  189.             needDisplay = GL_TRUE;
  190.             break;
  191.               case XK_f:
  192.               case XK_F:
  193.             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  194.             needDisplay = GL_TRUE;
  195.             break;
  196.               case XK_Left:
  197.             tx -= 0.1;
  198.             needDisplay = GL_TRUE;
  199.             break;
  200.               case XK_Right:
  201.             tx += 0.1;
  202.             needDisplay = GL_TRUE;
  203.             break;
  204.               case XK_Up:
  205.             ty += 0.1;
  206.             needDisplay = GL_TRUE;
  207.             break;
  208.               case XK_Down:
  209.             ty -= 0.1;
  210.             needDisplay = GL_TRUE;
  211.             break;
  212.               case XK_KP_Left:
  213.             rz += 1;
  214.             needDisplay = GL_TRUE;
  215.             break;
  216.               case XK_KP_Right:
  217.             rz -= 1;
  218.             needDisplay = GL_TRUE;
  219.             break;
  220.               case XK_KP_Up:
  221.             rx += 1;
  222.             needDisplay = GL_TRUE;
  223.             break;
  224.               case XK_KP_Down:
  225.             rx -= 1;
  226.             needDisplay = GL_TRUE;
  227.             break;
  228.               case XK_Escape:
  229.             return 0;
  230.             }
  231.         }
  232.         break;
  233.         }
  234.     } while (XPending(dpy) != 0);
  235.  
  236.     if (needDisplay) {
  237.         needDisplay = GL_FALSE;
  238.         DoDisplay(tx, ty, rx, rz);
  239.         glFlush();
  240.     }
  241.     }
  242. }
  243.